﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace DARE
{
    [AttrTypeDependentSerializable(typeof(CCubeEmitter))]
    public class CCubeEmitter: AEmitter
    {
        #region identifier

        public enum ESpawnType: uint
        {
            INTERNAL,
            FACES,
            EDGES
        }

        #endregion

        #region properties

        public Vector3 Cube { get; set; }
        public ESpawnType SpawnType { get; set; }

        #endregion

        #region ctor

        public CCubeEmitter(string name)
            :base(name)
        {

        }

        #endregion

        #region methods

        public override AEmitter Clone()
        {
            CCubeEmitter pe = new CCubeEmitter(Name);
            pe.EmissionInterval = EmissionInterval;
            pe.EmitPerEmission = EmitPerEmission;
            pe.MaxParticlesSimultaneous = MaxParticlesSimultaneous;
            pe.ReferenceParticle = ReferenceParticle;
            pe.Cube = Cube;
            pe.SpawnType = SpawnType;
            return pe;
        }

        public override void GetParametersFromEmitter(IParticle particle)
        {
            m_transformNode.Position = m_node.Position;
            m_transformNode.Orientation = m_node.Orientation;
            Vector3 cube = Cube;
            Vector3 posOff = m_node.Position;
            float x = CDare.Instance.Random.Next((int)(-cube.X * 1000), (int)(cube.X * 1000)) / 1000.0f;
            float y = CDare.Instance.Random.Next((int)(-cube.Y * 1000), (int)(cube.Y * 1000)) / 1000.0f;
            float z = CDare.Instance.Random.Next((int)(-cube.Z * 1000), (int)(cube.Z * 1000)) / 1000.0f;
            Vector3 vec = Vector3.One;//Vector3.Normalize(particle.Position - m_node.Position);

            switch (SpawnType)
            {
                case ESpawnType.INTERNAL:
                    break;
                case ESpawnType.FACES:
                    int face = CDare.Instance.Random.Next(3);
                    switch (face)
                    {
                        case 0:
                            if (CDare.Instance.Random.Next(2) == 1)
                                x = cube.X;
                            else
                                x = -cube.X;
                            vec = new Vector3(x, 0, 0);
                            break;
                        case 1:
                            if (CDare.Instance.Random.Next(2) == 1)
                                y = cube.Y;
                            else
                                y = -cube.Y;
                            vec = new Vector3(0, y, 0);
                            break;
                        case 2:
                            if (CDare.Instance.Random.Next(2) == 1)
                                z = cube.Z;
                            else
                                z = -cube.Z;
                            vec = new Vector3(0, 0, z);
                            break;
                    }

                    break;
                case ESpawnType.EDGES:
                    face = CDare.Instance.Random.Next(3);
                    switch (face)
                    {
                        case 0:
                            switch(CDare.Instance.Random.Next(4))
                            {
                                case 0:
                                    x = cube.X;
                                    y = cube.Y;
                                    break;
                                case 1:
                                    x = cube.X;
                                    y = -cube.Y;
                                    break;
                                case 2:
                                    x = -cube.X;
                                    y = cube.Y;
                                    break;
                                case 3:
                                    x = -cube.X;
                                    y = -cube.Y;
                                    break;
                            }
                            vec = new Vector3(x, y, 0);
                            break;
                        case 1:
                           switch(CDare.Instance.Random.Next(4))
                            {
                                case 0:
                                    x = cube.X;
                                    z = cube.Z;
                                    break;
                                case 1:
                                    x = cube.X;
                                    z = -cube.Z;
                                    break;
                                case 2:
                                    x = -cube.X;
                                    z = cube.Z;
                                    break;
                                case 3:
                                    x = -cube.X;
                                    z = -cube.Z;
                                    break;
                            }
                            vec = new Vector3(x, 0, z);
                            break;
                        case 2:
                            switch(CDare.Instance.Random.Next(4))
                            {
                                case 0:
                                    z = cube.Z;
                                    y = cube.Y;
                                    break;
                                case 1:
                                    z = cube.Z;
                                    y = -cube.Y;
                                    break;
                                case 2:
                                    z = -cube.Z;
                                    y = cube.Y;
                                    break;
                                case 3:
                                    z = -cube.Z;
                                    y = -cube.Y;
                                    break;
                            }
                            vec = new Vector3(0, y, z);
                            break;
                    }
                    break;
            }

            m_transformNode.Translate(new Vector3(x, y, z), CNode.ESpace.LOCAL);

            particle.Position = m_transformNode.Position;

                Vector3 dir = vec * Velocities;

                particle.Velocities = dir;
            
        }
        #endregion
    }
}
